package com.landian.springdata.demo.impl.service;

import com.landian.commons.base.dto.ResultDTO;
import com.landian.commons.base.utils.IdWorker;
import com.landian.springdata.demo.api.dto.OrderDTO;
import com.landian.springdata.demo.api.enums.OrderStatusEnum;
import com.landian.springdata.demo.api.enums.PayStatusEnum;
import com.landian.springdata.demo.api.service.OrderService;
import com.landian.springdata.demo.api.service.ProductInfoService;
import com.landian.springdata.demo.api.vo.DecreaseStockInput;
import com.landian.springdata.demo.api.vo.OrderDetail;
import com.landian.springdata.demo.api.vo.ProductInfo;
import com.landian.springdata.demo.impl.converter.OrderDetailConverter;
import com.landian.springdata.demo.impl.entity.OrderDetailEntity;
import com.landian.springdata.demo.impl.entity.OrderMasterEntity;
import com.landian.springdata.demo.impl.repository.OrderDetailRepository;
import com.landian.springdata.demo.impl.repository.OrderMasterRepository;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by 廖师兄
 * 2017-12-10 16:44
 * <a href="http://coding.imooc.com/class/74.html">网址</a>
 *
 * modify by caojl
 */
@Service
public class OrderServiceImpl implements OrderService{

    @Autowired
    private OrderMasterRepository orderMasterRepository;
    @Autowired
    private OrderDetailRepository orderDetailRepository;
    @Autowired
    private ProductInfoService productInfoService;

    @Override
    @Transactional(rollbackOn = Exception.class)
    public ResultDTO<String> create(OrderDTO orderDTO) {
        String orderId = IdWorker.getIdKey();
        //查询商品信息(调用商品服务)
        List<String> productIdList = orderDTO.getOrderDetailList().stream().map(OrderDetail::getProductId).collect(Collectors.toList());
        List<ProductInfo> productInfoList = productInfoService.findById(productIdList);
        //计算总价
        BigDecimal orderAmount = new BigDecimal(BigInteger.ZERO);
        for (OrderDetail orderDetail: orderDTO.getOrderDetailList()) {
            OrderDetailEntity orderDetailEntity = OrderDetailConverter.entity.convert(orderDetail);
            for (ProductInfo productInfo: productInfoList) {
                if (productInfo.getProductId().equals(orderDetailEntity.getProductId())) {
                    //单价*数量
                    orderAmount = productInfo.getProductPrice()
                            .multiply(new BigDecimal(orderDetailEntity.getProductQuantity()))
                            .add(orderAmount);
                    BeanUtils.copyProperties(productInfo, orderDetailEntity);
                    orderDetailEntity.setOrderId(orderId);
                    orderDetailEntity.setDetailId(IdWorker.getIdKey());
                    //订单详情入库
                    orderDetailRepository.save(orderDetailEntity);
                }
            }
        }
        //扣库存(调用商品服务)
        List<DecreaseStockInput> decreaseStockInputList = orderDTO.getOrderDetailList().stream()
                .map(e -> new DecreaseStockInput(e.getProductId(), e.getProductQuantity()))
                .collect(Collectors.toList());
        productInfoService.decreaseStock(decreaseStockInputList);
        //订单入库
        OrderMasterEntity masterEntity = OrderMasterEntity.builder().build();
        orderDTO.setOrderId(orderId);
        BeanUtils.copyProperties(orderDTO, masterEntity);
        masterEntity.setOrderAmount(orderAmount);
        masterEntity.setOrderStatus(OrderStatusEnum.NEW.getCode());
        masterEntity.setPayStatus(PayStatusEnum.WAIT.getCode());
        orderMasterRepository.save(masterEntity);
        return ResultDTO.success(orderId);
    }
}
